package com.atguigu.edumember.controller;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.edumember.entity.UcenterMember;
import com.atguigu.edumember.entity.vo.LoginInfoVo;
import com.atguigu.edumember.entity.vo.LoginVo;
import com.atguigu.edumember.entity.vo.RegisterVo;
import com.atguigu.edumember.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname MemberController
 * @Description TODO
 * @Date 2022/9/7 18:17
 * @Created by ZHL17
 */
@RestController
@RequestMapping("/edumember/member")
public class MemberController {
     @Autowired
     private UcenterMemberService memberService;
     @PostMapping("login")
     public R login(@RequestBody LoginVo loginVo){
      String token=memberService.login(loginVo);
      return R.ok().data("token",token);
     }
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        LoginInfoVo infoVo=memberService.getUserInfoById(memberId);
        return R.ok().data("userInfo",infoVo);
    }

    /**
     *  根据token中的id获取用户信息
     * @param
     * @return
     */
    @PostMapping("getUserInfo/{id}")
    public LoginInfoVo getInfo(@PathVariable("id") String id){
        UcenterMember member = memberService.getById(id);
        LoginInfoVo infoVo = new LoginInfoVo();
        BeanUtils.copyProperties(member,infoVo);
        return infoVo;
    }

    /**
     * 根据用户id 查找用户信息,生成订单信息
     * @param id
     * @return
     */
    @PostMapping("getMember/{id}")
    public UcenterMember getMember(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        return member;
    }

    /**
     *  统计的当天注册人数
     * @param day 日期
     * @return
     */
    @GetMapping("countRegister/{day}")
    public int countRegister(@PathVariable("day") String day){
        int count=memberService.countRegisterByDay(day);
        return count;
    }


}
